package com.java.servlet;

import com.java.bean.User;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author: wjr
 * @Description:
 * @Date Created in  2020-09-03 11:58
 * @Modified By:
 */

public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //接收参数
        String username = req.getParameter("username");
        String password = req.getParameter("password");

        User user = new User(username, password, 120, false);
        //数据校验伪代码 如果账号为user1，密码为123,则登录成功
        if ("user1".equals(user.getUserName()) && "123".equals(user.getPassWord())){
            //保存用户信息
            req.getSession().setAttribute("username",user.getUserName());
            req.getSession().setAttribute("user",user);
            req.getSession().setAttribute("num",user.getNum());
            //转发到成功界面
            resp.sendRedirect("/success.jsp");
        }
    }
}
